
import { app, dialog } from 'electron';
import fs from 'fs';
import path from 'path';
import { formatDate } from './utils';
// 获取用户的配置文件路径
const getUserDataPath = () => {
    return app.getPath('userData');
};
// 获取安装目录路径
const getInstallPath = () => {
    return app.getPath('exe').replace('YangShaoTool.exe', '');
}

const getNotesList = () => {
    let notesPath = path.join(getInstallPath(), 'notes');
    if(getInstallPath().indexOf('node_modules') > -1){
        notesPath = path.join(getUserDataPath(), 'notes');
    }
    const notesList = [];
    // 如果目录不存在，则创建目录
    if (!fs.existsSync(notesPath)) {
        fs.mkdirSync(notesPath, { recursive: true });
    }
    if (fs.existsSync(notesPath)) {
        const files = fs.readdirSync(notesPath);
        files.forEach(file => {
            const filePath = path.join(notesPath, file);
            const stat = fs.statSync(filePath);
            if (stat.isFile()) {
                const note = {
                    name: file,
                    path: filePath,
                    content: fs.readFileSync(filePath, 'utf-8'),
                    time: formatDate(stat.mtime)
                };
                notesList.push(note);
            }
        });
    }
    return notesList;
}

export {
    getNotesList
}